#include "temp_humi.h"

Temp_Humi::Temp_Humi(QWidget *parent) : QWidget(parent)
{
    aht20_init();




}


Temp_Humi::~Temp_Humi()
{

}




int Temp_Humi::aht20_init(void)
{
    aht20_fd = open(AHT20_DEV, O_RDWR);
    if(aht20_fd < 0) {
        //printf("can't open file %s\r\n", AHT20_DEV);
        qDebug() << "can not open file " << AHT20_DEV;
        return -1;
    }
    return aht20_fd;
}


int Temp_Humi::aht20_read(aht20_data_t *data)
{
    unsigned int databuf[2];
    int c1,t1;
    float hum,temp;
    int ret = 0;

    ret = read(aht20_fd,databuf, sizeof(databuf));

    if(ret == 0)
    {
        c1 = databuf[0]*1000/1024/1024;  //
        t1 = databuf[1] *200*10/1024/1024-500;
        hum = (float)c1/10.0;
        temp = (float)t1/10.0;

        data->humi = hum;
        data->temp = temp;
    //	printf("hum = %0.2f temp = %0.2f \r\n",hum,temp);
        //usleep(500000);
    }

    return ret;
}

